<?php
/*
	PookiWiki - A wiki unlike the rest.
	Copyright (C) 2012 Jordan Craw

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

	class PookiConnectionSettings
	{
		public $connection_type;
		public $URL;
		public $username;
		public $password;
		public $dbname;
	}

	class PookiSiteSettings
	{
		public $name;
		public $description;
		public $site_url;
	}
	
	class PookiSettings
	{
		protected $file_folder;
		protected $xml_parser;
		
		protected $connection_settings;
		protected $site_settings;

		public function simpleGetSettings()
		{
			return $this->connection_settings;
		}
		
		protected function fixSettings($setting)
		{
			
			if(count($setting))
			{
				$rec_array = array();
				foreach($setting as $setting => $setting_value)
				{
					$value = "";
					if(!count($setting_value))
					{
						$value = $setting_value;
					}
					$rec_array[$setting] = $value;
					
				}
				return $rec_array;
			}
			else
			{
				return "";
			}
		}
		// This is horrible, and I'm going to replace it with an XSL sheet, soon.
		protected function doSettingsCheck($contents)
		{
			$settings = new PookiXML('conf/settings.xml');
			
			if(count($settings->SiteSettings))
			{
				$siteSettings = $settings->SiteSettings;
				$TheseSettings = new PookiSiteSettings();
			
				$this->site_settings = new PookiSiteSettings();
				$this->site_settings->name = (string)$siteSettings["SiteName"];
				$this->site_settings->description = (string)$siteSettings["SiteDescription"];
				$this->site_settings->site_url = (string)$siteSettings["SiteURL"];
			}
			if(count($settings->ConnectionSettings))
			{
				$connSettings = $settings->ConnectionSettings;
				$this->connection_settings = new PookiConnectionSettings();
				$this->connection_settings->connection_type = (string)$connSettings["ConnectionType"];
				$this->connection_settings->connection_URL = (string)$connSettings["ConnectionURL"];
				$this->connection_settings->username = (string)$connSettings["ConnectionUserName"];
				
				$this->connection_settings->password = count($connSettings["ConnectionPassword"]) == 0 ? "" : array_shift($connSettings['ConnectionPassword']);
				$this->connection_settings->dbname = (string)$connSettings["DefaultDBName"];
			}
		}
		
		protected function checkSettingsFolder()
		{
			$this->file_folder		=	new PookiFile('conf');
			$filefolder 			= 	$this->file_folder; 
			$file_reader 			= 	new PookiFileReader($this->file_folder);
			$contents				=   $file_reader->contents_array;

			if(count($contents))
			{
				$contents 			= 	array_values(array_diff($contents,array(".",".."))); 
				$check				=	function($item)
										{
											return strpos($item,".xml");
										};
				$available			=	array_filter($contents,$check);
				
				if(count($available))
				{
					$settings_file 		= in_array("settings.xml",$available) ? "settings.xml" : array_pop($available);
					$file				= new PookiFile("conf/" . $settings_file);
					$file_reader		= new PookiFileReader($file);
					$content			= $file_reader;
					$this->doSettingsCheck($content);
				}
				else
				{
					throw new PookiException;
				}
			}
			
			unset($this->file_folder);
			
		}
	
	
		public function __construct()
		{
			$this->checkSettingsFolder();
			unset($this->XMLparser);
		}
	
	}

?>